<?php

declare(strict_types=1);

namespace App\Admin\Controller\Setting;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Service\Setting\SettingCrontabLogService;
use App\Admin\Service\Setting\SettingCrontabService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieController;
use support\Request;
use support\Response;

/**
 * 定时任务控制器
 * Class CrontabController
 * @package App\Setting\Controller\Tools
 */
#[Controller(prefix: "setting/crontab"), Auth]
class CrontabController extends NewbieController {
    /**
     * 计划任务服务
     */
    #[Inject]
    protected SettingCrontabService $service;

    /**
     * 计划任务日志服务
     */
    #[Inject]
    protected SettingCrontabLogService $logService;

    /**
     * 获取列表分页数据
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("setting:crontab:index")]
    public function index (Request $request): Response {
        return $this->success($this->service->getList($request->all()));
    }

    /**
     * 获取日志列表分页数据
     * @param Request $request
     * @return Response
     */
    #[GetMapping("logPageList")]
    public function logPageList (Request $request): Response {
        return $this->success($this->logService->getPageList($request->all()));
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    #[PostMapping("save"), Permission("setting:crontab:save")]
    public function save (Request $request): Response {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 立即执行定时任务
     * @param Request $request
     * @return Response
     */
    #[GetMapping("run"), Permission("setting:crontab:run")]
    public function run (Request $request): Response {
        $id = $request->input('id', null);
        if (is_null($id)) {
            return $this->error();
        } else {
            return $this->service->run($id) ? $this->success() : $this->error();
        }
    }

    /**
     * 获取一条数据信息
     * @param int $id
     * @return Response
     */
    #[GetMapping("read/{id}"), Permission("setting:crontab:read")]
    public function read (int $id): Response {
        return $this->success($this->service->read($id));
    }

    /**
     * 更新数据
     * @param int $id
     * @param Request $request
     * @return Response
     */
    #[PutMapping("update/{id}"), Permission("setting:crontab:update")]
    public function update (int $id, Request $request): Response {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量删除
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("delete/{ids}"), Permission("setting:crontab:delete")]
    public function delete (string $ids): Response {
        return $this->service->delete($ids) ? $this->success() : $this->error();
    }

    /**
     * 删除定时任务日志
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("deleteCrontabLog/{ids}"), Permission("setting:crontab:deleteCrontabLog")]
    public function deleteCrontabLog (string $ids): Response {
        return $this->logService->delete($ids) ? $this->success() : $this->error();
    }
}